package com.wzdigit.wms.wms.service.core.outbound;
/**
 * 拣货任务明细 服务类
 *
 * @Author:yangwenbiao
 * @Date 2021/2/26 4:06 PM
 * @version 1.0
 */

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wzdigit.wms.basic.domain.transfer.PicktaskDtl;
import com.wzdigit.wms.basic.client.request.picktask.PicktaskMangementRequest;
import com.wzdigit.wms.basic.client.response.picktask.PicktaskMangementResp;
import com.wzdigit.wms.basic.mapper.core.transfer.PicktaskDtlMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

@Service
@Slf4j
public class PicktaskDtlService {

    @Autowired
    PicktaskDtlMapper picktaskDtlMapper;

    public List<PicktaskDtl> getDtlListByPicktaskHeaderId(String loginName, Integer picktaskHeaderId) {

        //创建类型的条件构造器
        QueryWrapper<PicktaskDtl> wrapper = new QueryWrapper<>();
        wrapper.like("TASK_RECEIVER", loginName).eq("PICKTASK_HEADER_ID", picktaskHeaderId);

        List<PicktaskDtl> picktaskDtlList = picktaskDtlMapper.selectList(wrapper);
        return picktaskDtlList;
    }

    /**
     * 根据主键和行号
     * @param picktaskDtlId
     * @param picktaskLineNo
     * @return
     */
    public PicktaskDtl getByPkAndLineNo(Integer picktaskDtlId, Integer picktaskLineNo) {
        //创建类型的条件构造器
        QueryWrapper<PicktaskDtl> wrapper = new QueryWrapper<>();
        wrapper.eq("PICKTASK_DTL_ID", picktaskDtlId).eq("PICKTASK_LINE_NO", picktaskLineNo);

        PicktaskDtl picktaskDtl = picktaskDtlMapper.selectOne(wrapper);
        return picktaskDtl;
    }

    /**
     * 根据主键查询实体
     * @param picktaskDtlId
     * @return
     */
    public PicktaskDtl selectByPicktaskDtlId(Integer picktaskDtlId) {

        PicktaskDtl picktaskDtl = picktaskDtlMapper.selectById(picktaskDtlId);
        return picktaskDtl;
    }

    /**
     * 根据主键更新【实际拣货数】
     * @param picktaskDtlUpdate
     * @return
     */
    public Integer updateCmpQtyByPk(PicktaskDtl picktaskDtlUpdate) {

        int row = picktaskDtlMapper.updateById(picktaskDtlUpdate);
        return row;
    }

    /**
     * 根据主键修改状态
     * @param picktaskDtlId
     * @param code
     * @param loginName
     * @param now
     * @return
     */
    @Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRED)
    public Integer updateStatusByPk(Integer picktaskDtlId, int code, String loginName, Date now) {

        PicktaskDtl picktaskDtl = new PicktaskDtl();
        picktaskDtl
                .setPicktaskDtlId(picktaskDtlId)
                .setStatus(code);
        picktaskDtl.setModifyUser(loginName);
        picktaskDtl.setModifyTime(now);

        int updateRow = picktaskDtlMapper.updateById(picktaskDtl);

        return updateRow;
    }

    /**
     * 根据单头ID查询明细集合
     * @param picktaskHeaderId
     * @return
     */
    public List<PicktaskDtl> getDtlByHeadId(Integer picktaskHeaderId) {

        //创建类型的条件构造器
        QueryWrapper<PicktaskDtl> wrapper = new QueryWrapper<>();
        wrapper.eq("PICKTASK_HEADER_ID", picktaskHeaderId);
        List<PicktaskDtl> picktaskDtlList = picktaskDtlMapper.selectList(wrapper);

        return picktaskDtlList;
    }

    public List<PicktaskMangementResp> getTaskMmanagementInfo(PicktaskMangementRequest picktaskMangementRequest) {

        List<PicktaskMangementResp> picktaskMangementRespList = picktaskDtlMapper.getTaskMmanagementInfo(picktaskMangementRequest);
        return picktaskMangementRespList;
    }

    /**
     * 更新明细的任务领取人
     * @param picktaskDtlId
     * @param username
     * @param loginName
     * @param date
     * @return
     */
    public Integer updateTaskReceiverOnPicktaskDtlId(Integer picktaskDtlId, String username, String loginName, Date date) {


        PicktaskDtl picktaskDtl = new PicktaskDtl();
        picktaskDtl.setModifyTime(date);
        picktaskDtl.setModifyUser(loginName);

        picktaskDtl.setPicktaskDtlId(picktaskDtlId).setTaskReceiver(username);

        int row = picktaskDtlMapper.updateById(picktaskDtl);

        return row;
    }

    /**
     * 新增一条记录
     * @param picktaskDtl
     * @return
     */
    public Integer addOne(PicktaskDtl picktaskDtl) {

        int row = picktaskDtlMapper.insert(picktaskDtl);
        return row;
    }

    /**
     * 根据生成拣货任务的明细ID和其类别查询拣货明细
     * @param docDtlId
     * @param docType
     */
    public List<PicktaskDtl> getByDocDtlIdAndDocType(Long docDtlId, String docType) {
        //创建类型的条件构造器
        QueryWrapper<PicktaskDtl> wrapper = new QueryWrapper<>();
        wrapper.eq("DOC_DTL_ID", docDtlId);
        wrapper.eq("DOC_TYPE", docType);


        List<PicktaskDtl> picktaskDtlList = picktaskDtlMapper.selectList(wrapper);
        return picktaskDtlList;
    }

    /**
     * 根据明细ID查询明细集合
     * @param dtlIdList
     * @return
     */
    public List<PicktaskDtl> selectByPicktaskDtlIdList(List<Integer> dtlIdList) {
        //创建类型的条件构造器
        QueryWrapper<PicktaskDtl> wrapper = new QueryWrapper<>();
        wrapper.in("PICKTASK_DTL_ID", dtlIdList);

        List<PicktaskDtl> picktaskDtlList = picktaskDtlMapper.selectList(wrapper);
        return picktaskDtlList;
    }

    public Integer updateCmpQtyByDtlId(PicktaskDtl picktaskDtlUpdate, BigDecimal qty) {
        Integer row = picktaskDtlMapper.updateCmpQtyByDtlId(picktaskDtlUpdate, qty);
        return row;
    }

    public PicktaskDtl getDtlByHeadIdAndLineNo(Integer picktaskHeaderId, String picktaskLineNo) {
        //创建类型的条件构造器
        QueryWrapper<PicktaskDtl> wrapper = new QueryWrapper<>();
        wrapper.eq("PICKTASK_HEADER_ID", picktaskHeaderId);
        wrapper.eq("PICKTASK_LINE_NO", picktaskLineNo);

        PicktaskDtl picktaskDtl = picktaskDtlMapper.selectOne(wrapper);
        return picktaskDtl;
    }

    public Integer updateSplit(PicktaskDtl picktaskDtl, BigDecimal splitQty) {
        int row = picktaskDtlMapper.updateSplit(picktaskDtl, splitQty);
        return row;
    }

    public Integer updateByPk(PicktaskDtl picktaskDtlUp) {

        int i = picktaskDtlMapper.updateById(picktaskDtlUp);
        return i;
    }
}
